package com.example.fragment;

import cn.jpush.android.api.JPushInterface;

import com.example.activity.CaptureActivity;
import com.example.activity.MessageInfoActivity;
import com.benchi.ncar.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.umeng.update.UmengUpdateAgent;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class IMainActivity extends FragmentActivity {
	@ViewInject(R.id.rg_operation)
	private RadioGroup mRgOperation;
	@ViewInject(R.id.rb_shop)
	private RadioButton mRbShop;
	@ViewInject(R.id.rb_palmtop)
	private static RadioButton mRbPalm;
	@ViewInject(R.id.v_new_msg)
	public static View mVNewMsg;

	private FragmentManager mFragmentManager;
	private HomeFragment mHomeFragment;
	private UserFragment mUserFragment;
	private PalmtopFragment mPalmtopFragment;
	private BrandFragment mBrandFragment;
	protected long mLastTime = 0;
	private static Bundle mBundle;
	public static boolean mIsNewMsg = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ViewUtils.inject(this);
		rgOperation();
		mFragmentManager = getSupportFragmentManager();
		mRbShop.setChecked(true);
		UmengUpdateAgent.update(this);
		String url = getIntent().getStringExtra("url");
		if (url != null && !url.equals("")) {
			MessageInfoActivity.skipMe(this, url);
		}

		if (mIsNewMsg) {
			mVNewMsg.setVisibility(View.VISIBLE);
		} else {
			mVNewMsg.setVisibility(View.GONE);
		}
	}

	private void hideFragments(FragmentTransaction transaction) {
		if (mHomeFragment != null) {
			transaction.hide(mHomeFragment);
		}
		if (mPalmtopFragment != null) {
			transaction.hide(mPalmtopFragment);
		}
		if (mBrandFragment != null) {
			transaction.hide(mBrandFragment);
		}
		if (mUserFragment != null) {
			transaction.hide(mUserFragment);
		}
	}

	@OnClick(R.id.img_erweima)
	public void erweima(View v) {
		Intent intent = new Intent(IMainActivity.this, CaptureActivity.class);
		startActivity(intent);
	}

	private void rgOperation() {
		mRgOperation.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup arg0, int id) {
				FragmentTransaction fragmentTran = mFragmentManager
						.beginTransaction();
				// hideFragments(fragmentTran);
				switch (id) {
				case R.id.rb_shop:
					// if (mHomeFragment == null) {
					mHomeFragment = new HomeFragment();
					fragmentTran.replace(R.id.fl_main, mHomeFragment);
					// }
					// fragmentTran.show(mHomeFragment);
					break;
				case R.id.rb_palmtop:
					// if (mPalmtopFragment == null) {
					mPalmtopFragment = new PalmtopFragment();
					if (mBundle != null) {
						mPalmtopFragment.setArguments(mBundle);
					}
					fragmentTran.replace(R.id.fl_main, mPalmtopFragment);
					mBundle = null;
					// }
					// fragmentTran.show(mPalmtopFragment);
					break;
				case R.id.rb_brand:
					// if (mBrandFragment == null) {
					mBrandFragment = new BrandFragment();
					fragmentTran.replace(R.id.fl_main, mBrandFragment);
					// }
					// fragmentTran.show(mBrandFragment);
					break;
				case R.id.rb_user:
					// if (mUserFragment == null) {
					mIsNewMsg = false;
					mVNewMsg.setVisibility(View.GONE);
					mUserFragment = new UserFragment();
					fragmentTran.replace(R.id.fl_main, mUserFragment);
					// }
					// fragmentTran.show(mUserFragment);
					break;
				}
				fragmentTran.commit();
			}
		});

	}

	public static Handler handle = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				mBundle = msg.getData();
				mRbPalm.setChecked(true);
				break;
			}
		};
	};

	/**
	 * back键拦截
	 */
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			long mSecondTime = System.currentTimeMillis();
			if (mSecondTime - mLastTime > 800) {// 如果两次按键时间间隔大于800毫秒，则不退出
				Toast.makeText(IMainActivity.this, "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
				mLastTime = mSecondTime;// 更新mLastTime
				return true;
			} else {
				System.exit(0);// 否则退出程序
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	protected void onPause() {
		super.onPause();
		JPushInterface.onPause(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		JPushInterface.onResume(this);
	}
}
